package com.zpark.wxshop.service.impl;


import com.zpark.wxshop.Vo.OrderInfoVo;
import com.zpark.wxshop.entity.Order;
import com.zpark.wxshop.mapper.OrderMapper;
import com.zpark.wxshop.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PJone
 * @since 2023-09-07
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    /** 设置集合名称 */
    private static final String COLLECTION_NAME = "OrderInfoVos";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 支付后获取到redis中订单
     */
    public Boolean SaveOrders(String orderid){
        //查询mongodb数据库中是否有相同的文档,如果有则删除
        if (mongoTemplate.findOne(new Query(Criteria.where("orderid").is(orderid)), OrderInfoVo.class, COLLECTION_NAME) != null)
            mongoTemplate.remove(new Query(Criteria.where("orderid").is(orderid)), COLLECTION_NAME);
        //添加文档到mongodb数据库中
        OrderInfoVo orderInfoVo = (OrderInfoVo) Objects.requireNonNull(redisTemplate.opsForValue().get("Card:checkProducts:" + orderid));
        mongoTemplate.save(orderInfoVo, COLLECTION_NAME);
        return true;
    }

    /**
     * 查询用户所有订单
     */
    public OrderInfoVo FindOrderByOrderid(String orderid){
        //得到该用户的订单码列表
        return mongoTemplate.findOne(new Query(Criteria.where("orderid").is(orderid)), OrderInfoVo.class, COLLECTION_NAME);
    }

}
